package com.badger.generator;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
/**
 * 写文档工具类  可以配合notepad++（JSON格式化）
 * @author fupc
 *
 */
public class dataFormatutil {
	
	    /**
	     * @author Howe
	     * @Description 将驼峰转为下划线
	     * @param str
	     * @return java.lang.String
	     * @Date   2022/4/22 13:11
	     * @since  1.0.0
	     */
	    public static String xX2x_x(String str) {
	        Pattern compile = Pattern.compile("[A-Z]");
	        Matcher matcher = compile.matcher(str);
	        StringBuffer sb = new StringBuffer();
	        while(matcher.find()) {
	            matcher.appendReplacement(sb,  "_" + matcher.group(0).toLowerCase());
	        }
	        matcher.appendTail(sb);
	        return sb.toString();
	    }

	    /**
	     * @author Howe
	     * @Description 将下划线转为驼峰
	     * @param str
	     * @return java.lang.String
	     * @Date   2022/4/22 13:12
	     * @since  1.0.0
	     */
	    public static String x_x2xX(String str) {
	        str = str.toLowerCase();
	        Pattern compile = Pattern.compile("_[a-z]");
	        Matcher matcher = compile.matcher(str);
	        StringBuffer sb = new StringBuffer();
	        while(matcher.find()) {
	            matcher.appendReplacement(sb,  matcher.group(0).toUpperCase().replace("_",""));
	        }
	        matcher.appendTail(sb);
	        return sb.toString();
	    }
	
	
	/**
	 * 将json格式的数据获取key，获取val，分行显示，用于写文档
	 * @param args
	 */
	public static void jsonFormat(String jsonstr) {
		 jsonstr="{\r\n" + 
				"		    \"index\": null,\r\n" + 
				"		    \"puResultId\": \"b235b2e28ff34a4e92e59e07e18e826a\",\r\n" + 
				"		    \"settMonth\": \"202311\",\r\n" + 
				"		    \"marketEntityNameEge\": \"广东省测试内网导入发电企业\",\r\n" + 
				"		    \"itemName\": \"广东省测试专用光伏项目一\",\r\n" + 
				"		    \"marketEntityNamePu\": \"1123认购用户\",\r\n" + 
				"		    \"marketingUserNoPu\": null,\r\n" + 
				"		    \"marketArea\": \"省内-广东\",\r\n" + 
				"		    \"useEnergy\": 60000,\r\n" + 
				"		    \"settleEnergy\": 40000,\r\n" + 
				"		    \"electricityPrice\": 3000,\r\n" + 
				"		    \"envAddPrice\": 100,\r\n" + 
				"		    \"envAddCost\": 4000000,\r\n" + 
				"		    \"marketEntityIdPu\": \"c917f0858c8543cf84a58d5d7a24d915\",\r\n" + 
				"		    \"marketEntityNoPu\": \"GP01GD000016\"\r\n" + 
				"		}";
		
		String fdsa[]=jsonstr.split(":");
		System.out.println(fdsa[0]);
		
	}
	
	
	public static String getkey(String json) {
		JSONObject j = JSON.parseObject(json);
		//JSONObject j = new JSONObject(JSON.parseObject(json));
		//将json转换为key=value&key=value&...的形式
		StringBuilder sb = new StringBuilder();
		String sbString="";
		try {
			
			Set<String> iterators = j.keySet();
			Iterator<String> iterator= iterators.iterator();
			int i=0;
			while(iterator.hasNext()){
		String key = (String) iterator.next();
			    	sb.append(key+"\n");
			    	//sb.append("="+jsonObject.getString(key));
			    	i++;
			}
			sbString = sb.toString();
			System.out.println("一共字段数：  "+i);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sbString;
	}
	
	public static String getval(String json) {
		  JSONObject j = JSON.parseObject(json);

		  //将json转换为key=value&key=value&...的形式
		  StringBuilder sb = new StringBuilder();
		  String sbString="";
		  try {

		   Set<String> iterators = j.keySet();
		   Iterator<String> iterator= iterators.iterator();
		   int i=0;
		   while(iterator.hasNext()){
		    String key = (String) iterator.next();
		    sb.append(j.getString(key)+"\n");
		    i++;
		   }
		   //sb.append("&key");
		   sbString = sb.toString();
		   System.out.println("一共字段数：  "+i);
		  } catch (Exception e) {
		   e.printStackTrace();
		  }
		  //System.out.println(sbString);
		  return sbString;
		 }
	

	public static void main(String[] args) {
		//key 值相同会选最后一个value
		String jsonstr="{\r\n" + 
				"				\"marketingNo\": \"市场主体编号\",\r\n" + 
				"				\"usePowerAddress\": \"工商注册所在地\",\r\n" + 
				"				\"powerGrid\": \"所属电网\",\r\n" + 
				"				\"marketingUserNo\": \"营销用户号\"\r\n" + 
				"			}";
		
		String fdsa=getkey(jsonstr );
		System.out.println(fdsa);
		System.out.println("---------------------------------\n");
		String ssd=xX2x_x( fdsa);
		System.out.println(ssd);
		String value=getval( jsonstr);
		System.out.println(value);
	
	}

}
